package queue

import "errors"

type ArrQueueV1 struct {
	size     int
	capacity int
	Data     []any
	front    int
}

func NewArrQueueV1(capacity int) *ArrQueueV1 {
	return &ArrQueueV1{
		size:     0,
		capacity: capacity,
		Data:     make([]any, capacity),
	}
}

func (a *ArrQueueV1) Size() int {
	return a.size
}

func (a *ArrQueueV1) Capacity() int {
	return a.capacity
}

func (a *ArrQueueV1) Push(val any) error {
	if a.size == a.capacity {
		return errors.New("failure: capacity is full")
	}

	index := (a.front + a.size) % a.capacity
	a.Data[index] = val
	a.size++
	return nil
}

func (a *ArrQueueV1) IsEmpty() bool {
	return a.size <= 0
}

func (a *ArrQueueV1) Pop() any {
	if a.size <= 0 {
		return nil
	}

	val := a.Data[a.front]
	a.size--

	a.front = (a.front + 1) % a.capacity
	return val
}

func (a *ArrQueueV1) Peek() any {
	if a.IsEmpty() {
		return nil
	}

	return a.Data[a.front]
}
